﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 02/04/2014
 * Time: 05:43 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using Gestión_UTN.Entities;
using System.Collections.Generic;

namespace Gestión_UTN.daimpl
{
	/// <summary>
	/// Description of cursadadaimpl.
	/// </summary>
	public class cursadadaimpl:Gestión_UTN.da.cursadada
	{
		public cursadadaimpl()
		{
		}
		
		public bool SaveCursada(int anio, int cuatrimestre)
		{
			bool ingresada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "INSERT INTO cur_cursadas(cur_anio, cur_cuatrimestre)" +
                    			"VALUES(@anio, @cuatrimestre)";
					
					OleDbCommand insertar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					insertar.Parameters.AddWithValue("@anio",anio);
					insertar.Parameters.AddWithValue("@cuatrimestre",cuatrimestre);
					
					insertar.ExecuteNonQuery();
					
					trans.Commit();
					
					ingresada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				ingresada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return ingresada;
		}
		
		public bool DeleteCursada(int id)
		{
			bool eliminada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "DELETE FROM cur_cursadas " +
						"WHERE cur_id = @cur_id";
					
					OleDbCommand eliminar = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					trans = conex.BeginTransaction();
					
					eliminar.Parameters.AddWithValue("@cur_id",id);
					
					eliminar.ExecuteNonQuery();
					
					trans.Commit();
					
					eliminada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				eliminada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return eliminada;
		}
		
		public bool ModifyCursada(int id, int anio, int cuatrimestre)
		{
			bool modificada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "UPDATE cur_cursadas SET cur_anio = @anio, cur_cuatrimestre = @cuatrimestre " +
									"WHERE cur_id = @cur_id";
					
					OleDbCommand modificar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					object[] values = {anio,cuatrimestre,id};
					
					modificar.Parameters.AddRange(values);
					
					modificar.ExecuteNonQuery();
					
					trans.Commit();
					
					modificada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				modificada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return modificada;
		}
		
		public System.Data.DataTable FindAllCursadas()
		{
			DataTable tablaCursadas = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				tablaCursadas = new DataTable();
				
				if (null != conex) {
					
					string sql = "SELECT cur_id, cur_anio, cur_cuatrimestre" +
									" FROM cur_cursadas cur";
					
					 OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);
					 
					 conex.Open();
					 
					 adaptador.Fill(tablaCursadas,"cursadas");
					
					 conex.Close();
				}
				
			} catch (Exception) {
				
				tablaCursadas = null;
				conex.Close();
			}
			
			return tablaCursadas;
		}
		
		public System.Collections.Generic.List<Gestión_UTN.Entities.Cursada> FindCursadasByAnio(int anio)
		{
			List<Cursada> cursadas = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				
				if (null != conex) {
					
					string sql = "SELECT cur_id, cur_anio, cur_cuatrimestre" +
									" FROM cur_cursadas cur" +
									" WHERE cur_anio = @anio";
					
					OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					consulta.Parameters.AddWithValue("@anio",anio);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					cursadas = new List<Cursada>();
					
					while (lector.Read()) 
					{
						Cursada cur = new Cursada(Convert.ToInt16(lector[0].ToString()),
						                           Convert.ToInt16(lector[1].ToString()),
						                           Convert.ToInt16(lector[2].ToString()));
						cursadas.Add(cur);
						cur = null;                                
					}
					
					conex.Close();
				}
				
			} catch (Exception) {
				
				cursadas = null;
				conex.Close();
			}
			
			return cursadas;
		}
		
		public Cursada FindCursadasById(int id)
		{
			Cursada cursada = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				
				if (null != conex) {
					
					string sql = "SELECT cur_id, cur_anio, cur_cuatrimestre" +
									" FROM cur_cursadas cur" +
									" WHERE cur_id = @id";
					
					OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					consulta.Parameters.AddWithValue("@id",id);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					while (lector.Read()) 
					{
						cursada = new Cursada(Convert.ToInt16(lector[0].ToString()),
						                           Convert.ToInt16(lector[1].ToString()),
						                           Convert.ToInt16(lector[2].ToString()));                        
					}
					
					conex.Close();
				}
				
			} catch (Exception) {
				
				cursada = null;
				conex.Close();
			}
			
			return cursada;
		}
	}
}
